package com.tj.collection.controller.audit;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.common.utils.HttpUtil;
import com.fqgj.common.utils.StringUtils;
import com.google.common.collect.Maps;
import com.kly.user.service.UserService;
import com.tj.collection.bean.ApiResponse;
import com.tj.collection.controller.audit.request.ProfileAuditReq;
import com.tj.collection.controller.audit.request.ProfileListReq;
import com.tj.collection.controller.audit.response.ProfileResponse;
import com.tj.collection.db.service.ProfileAuditService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/profile")
public class ProfileAuditController {

    @Resource
    ProfileAuditService profileAuditService;
    @Resource
    UserService userService;
    @NacosValue(value = "${chatter.url}", autoRefreshed = true)
    private String chatterApiUrl;
    /**
     * Profile审核列表
     */
    @RequestMapping("/audit/list")
    public ApiResponse profileList(@RequestBody ProfileListReq request) {

//        final int totalNum = profileAuditService.getProfileAuditCnt(req.getChatterAccount(), req.getModelUserCode(), req.getApprovalStatus());
//        if (totalNum == 0) {
//            return new ApiResponse(ProfileResponse.builder().totalNum(0).build());
//        }
//
//        final List<ChatterModelInfoExamine> chatterModelInfoExamines = profileAuditService.listProfileAudit(req.getChatterAccount(), req.getModelUserCode(), req.getApprovalStatus(), new Page(req.getPageSize(), req.getCurrent()));
//        final List<ProfileVo> userProfileAudits = chatterModelInfoExamines.stream().map(o -> {
//            ProfileVo vo = ProfileVo.builder().build();
//            BeanUtils.copyProperties(o, vo);
//            vo.setApprovalStatus(o.getOperateStatus());
//            vo.setModelUserCode(o.getModelUserCode());
//            vo.setChatterNickName(o.getChatterNickName());
//            vo.setDisableFlag(o.getOperateStatus() == 2 || o.getOperateStatus() == 3);
//
//            return vo;
//        }).collect(Collectors.toList());

        Map<String, String> header = Maps.newHashMap();
        header.put("Content-Type", "application/json");

        final String response = HttpUtil.postPageBody(chatterApiUrl + "/profile/audit/list", header, JSONObject.toJSONString(request));

        if (StringUtils.isEmpty(response)) {
            return new ApiResponse();
        }
        final JSONObject responseJson = JSONObject.parseObject(response);
        if (responseJson.getIntValue("code") != 0) {
            return new ApiResponse();
        }

        final String data = responseJson.getString("data");
        if (StringUtils.isEmpty(data)) {
            return new ApiResponse();
        }

        final ProfileResponse profileResponse = JSONObject.parseObject(data, ProfileResponse.class);

        return new ApiResponse(profileResponse);
    }

    /**
     * Profile审核
     */
    @RequestMapping("/audit")
    public ApiResponse profileAudit(@RequestBody ProfileAuditReq request) {

        Map<String, String> header = Maps.newHashMap();
        header.put("Content-Type", "application/json");

        final String response = HttpUtil.postPageBody(chatterApiUrl + "/profile/audit", header, JSONObject.toJSONString(request));

        if (StringUtils.isEmpty(response)) {
            return new ApiResponse();
        }
        final JSONObject responseJson = JSONObject.parseObject(response);
        if (responseJson.getIntValue("code") != 0) {
            return new ApiResponse().setCode(500).setMsg("Audit Failed");
        }

        final String data = responseJson.getString("data");
        if (StringUtils.isEmpty(data)) {
            return new ApiResponse();
        }

        return new ApiResponse();
    }
}